Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More precise error messages #301

Merged
merged 9 commits into from
Aug 31, 2018

Conversation

mamazu
Copy link
Member

@mamazu mamazu commented Aug 27, 2018

As I have already mentioned (#296) I would suggest to use more precise error codes in the address book api to indicate for example that the user is not logged in. I have also added a PHPSpec test for the CurrentlyLoggedInUserProvider

@mamazu
Copy link
Member Author

mamazu commented Aug 30, 2018

Please rerun the build.

400:
description: "Validation failed"
schema:
$ref: "#/definitions/GeneralError"
401:
description: "No user is logged in"
500:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be 404, I guess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you may have noticed I have added authorization symbols to the documentation. And the address book is something that only logged in users can see. If you are not logged in and try to call a route where you need to be logged in I wanted to return a 401 Unauthorized.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it is not the scope of this PR anyway?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in the documentation? That is already implemented. But the only parts that are behind a login wall are the address book and some of the customer endpoints.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was just confused, that we are handling error with 500 code. But this is totally not part of this PR. I'm just missing some part why, but this is rather general question about current codebase :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, the problem with the API was that assertions throw exceptions which are not very helpful to a user.

*/
private $tokenStorage;
private $currentUserProvider;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should adjust the name of field as well

* @param CommandBus $bus
* @param TokenStorageInterface $tokenStorage
* @param CurrentUserProviderInterface $currentUserProvider
* @param ViewHandlerInterface $viewHandler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No doc comment variable alignment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have resigned from this practice, as it is problematic once you want to add a new argument to docblock. People tend to forget about alignment and even if not, then diffs are less readable. Can you revert these changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only manually, but I will.

$this->client->request('GET', '/shop-api/address-book', [], [], ['ACCEPT' => 'application/json']);
$response = $this->client->getResponse();

$this->assertResponse($response, 'address_book/show_address_book_unauthorized_response', Response::HTTP_UNAUTHORIZED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just assert response code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

*/
private $currentUserProvider;

/**
* AddressExistsValidator constructor.
*
* @param AddressRepositoryInterface $addressRepository
* @param CurrentUserProviderInterface $currentUserProvider
* @param AddressRepositoryInterface $addressRepository
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be reversed

@@ -18,19 +18,19 @@
*/
private $addressRepository;
/**
* @var CurrentUserProviderInterface
* @var LoggedInUserProviderInterface
*/
private $currentUserProvider;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rename this parameter in the whole codebase

@@ -9,12 +9,20 @@
final class RemoveAddressRequest
{
/**
* @var mixed
* @var int|string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👍

{
/**
* @var TokenStorage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -77,9 +77,11 @@ public function __invoke(Request $request): Response
$updateCustomerCommand = $updateCustomerRequest->getCommand();
$this->bus->handle($updateCustomerCommand);

return $this->viewHandler->handle(View::create(
return $this->viewHandler->handle(
View::create(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be reverted or arguments of create method should be indented

$this->addressBookViewFactory->create($updatedAddress, $user->getCustomer()),
Response::HTTP_OK
);
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of else we should use early return approach. Using return $this->viewHandler->handle(...); with two different arguments should be easier to understand what is happening here

return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED));
}

if (($customer = $user->getCustomer()) !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed doubled brackets

TokenStorageInterface $tokenStorage,
TokenInterface $token,
ShopUserInterface $shopUser
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS

@lchrusciel lchrusciel merged commit 4a51f18 into Sylius:master Aug 31, 2018
@lchrusciel
Copy link
Member

Thanks @mamazu! 🐋

@mamazu mamazu deleted the more_precise_error_messages branch September 12, 2018 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants